package server.content.skills.fm;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.python.modules.time.Time;

import core.util.Misc;

import server.Server;
import server.clip.region.Region;
import server.event.CycleEvent;
import server.event.CycleEventContainer;
import server.event.CycleEventHandler;
import server.event.Event;
import server.event.EventContainer;
import server.event.EventManager;
import server.game.players.Client;

public class MiracleFM {

	private final Client c;
	private final Map<Integer, Integer> lvlReqs = new HashMap<Integer, Integer>();
	private final Map<Integer, Integer> logXp = new HashMap<Integer, Integer>();
	private int fireTick = -1;
	private GlobalFire currentFire = null;
	private boolean isFiremaking;

	public MiracleFM(final Client c) {
		this.c = c;
		/*
		 * Level Requirments.
		 */
		lvlReqs.put(1511, 1);
		lvlReqs.put(1521, 15);
		lvlReqs.put(1519, 30);
		lvlReqs.put(1517, 45);
		lvlReqs.put(1515, 60);
		lvlReqs.put(1513, 75);
		/*
		 * Log XPs.
		 */
		logXp.put(1515, 290);
		logXp.put(1517, 250);
		logXp.put(1511, 115);
		logXp.put(1521, 165);
		logXp.put(1519, 210);
		logXp.put(1513, 340);

	}

	public void startFire(final int logId, final int logSlot) {
		if (currentFire != null) {
			return;
		}
		final int skillLvl = c.playerLevel[11];
		if (fireTick > 0) {
			c.stopMovement();
			return;
		}
		if (skillLvl >= getLvlReq(logId)) {
			if (Server.gfh.fireExists(c.getX(), c.getY())) {
				c.sendMessage("There is already a fire burning here...");
				resetFM();
				return;
			}
		
			setIsFiremaking(true);
			Server.itemHandler.createGroundItem(c, logId, c.getX(), c.getY(),
					1, c.getId());
			c.getItems().deleteItem(logId, 1);
			c.startAnimation(733);
			c.getPA().requestUpdates();
			c.startAnimation(733);
			c.getPA().requestUpdates();
			final int expire = Misc.random(150);
			if (expire > 29) {
				currentFire = new GlobalFire(c, logId, c.getX(), c.getY(),
						expire);
			} else {
				currentFire = new GlobalFire(c, logId, c.getX(), c.getY(), 37);
			}
			fireTick = Misc.random(7);
			if (fireTick < 4) {
				fireTick = 6;
			}
			setIsFiremaking(true);
		} else {
			c.sendMessage("You need a firemaking level of " + getLvlReq(logId)
					+ " to burn these logs.");
		}
	}

	public void createFire(final int x, final int y, final int expire) {
		Server.objectHandler.createAnObject(c, 2732, x, y);
		Server.gfh.addFire(currentFire);
	}

	public void processFires() {
		if (!isFiremaking())
			return;
		if (fireTick > 0) {
			fireTick--;
		} else if (fireTick == 0) {
			Server.itemHandler.removeGroundItem(c, currentFire.getLogId(),
					c.getX(), c.getY(), false);
			createFire(c.getX(), c.getY(), 75);
			c.startAnimation(65535);
			c.getPA().requestUpdates();
			final int XP = logXp.get(currentFire.getLogId());
			c.getPA().addSkillXP(XP, 11);
			c.sendMessage("The logs begin to catch fire.");
			if (Region
					.getClipping(c.getX() - 1, c.getY(), c.heightLevel, -1, 0)) {
				c.getPA().walkTo(-1, 0);
			} else if (Region.getClipping(c.getX() + 1, c.getY(),
					c.heightLevel, 1, 0)) {
				c.getPA().walkTo(1, 0);
			} else if (Region.getClipping(c.getX(), c.getY() - 1,
					c.heightLevel, 0, -1)) {
				c.getPA().walkTo(0, -1);
			} else if (Region.getClipping(c.getX(), c.getY() + 1,
					c.heightLevel, 0, 1)) {
				c.getPA().walkTo(0, 1);
			}
			resetFM();
		}
	}

	public void resetFM() {
		fireTick = -1;
		currentFire = null;
		setIsFiremaking(false);
	}

	public void setIsFiremaking(final boolean isFiremaking) {
		this.isFiremaking = isFiremaking;
	}

	public boolean isFiremaking() {
		return isFiremaking;
	}

	private static final int logs[] = { 1511, 1521, 1519, 1515, 1513, 1517 };

	public static boolean isLog(final int id) {
		for (int i = 0; i < logs.length; i++) {
			if (logs[i] == id) {
				return true;
			}
		}
		return false;
	}

	public int getLvlReq(final int logId) {
		return lvlReqs.get(logId);
	}

}
